from pynput.keyboard import Key, Controller  # 导入按键控制
import time  # 导入时间包，调用sleep函数让琴键有时间间隔
import threading  # 导入线程包
from pywinauto import Desktop


# 演奏函数
def play_piano(music):
    for n in music:
        if n.isupper():
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press(n.lower())
            time.sleep(0.479)
            keyboard.release(n.lower())
            keyboard.release(Key.shift)
        elif n == "|" or n == ")":
            pass
        elif n in "!@$%^*(":
            keyboard.press(Key.shift)
            time.sleep(0.001)
            keyboard.press("1245689"["!@$%^*(".index(n)])
            time.sleep(0.479)
            keyboard.release("1245689"["!@$%^*(".index(n)])
            keyboard.release(Key.shift)
        elif n != " " and n != "-":
            keyboard.press(n)
            if music.index(n) != len(music) - 1 and music[music.index(n) + 1] == ")":
                time.sleep(0.24)
            else:
                time.sleep(0.48)
            keyboard.release(n)
        elif n == "-":
            time.sleep(0.96)
        else:
            time.sleep(0.48)


def thread_play():
    # 运行线程
    rt = threading.Thread(target=play_piano, args=(right,))  # 主旋律线程
    lt = threading.Thread(target=play_piano, args=(left,))  # 和弦线程
    rtt = threading.Thread(target=play_piano, args=(rightThumb,))  # 右手拇指线程
    rift = threading.Thread(target=play_piano, args=(rightIndexFinger,))  # 右手食指线程
    ltt = threading.Thread(target=play_piano, args=(leftThumb,))  # 左手拇指线程
    lift = threading.Thread(target=play_piano, args=(leftIndexFinger,))  # 右手食指线程
    rt.start()
    lt.start()
    rtt.start()
    rift.start()
    ltt.start()
    lift.start()
    rt.join()
    lt.join()
    rtt.join()
    rift.join()
    ltt.join()
    lift.join()


if __name__ == '__main__':
    # 准备演奏
    # 控制键盘
    keyboard = Controller()
    # 链接的方式点击桌面任务栏的正在运行程序print_control_identifiers()
    dlg = Desktop(backend="uia").任务栏.运行中的程序.child_window(title="Google Chrome - 1 个运行窗口", auto_id="Chrome",
                                                         control_type="Button").click()
    time.sleep(2)
    # 右手
    right = "--uosf|f-gi- |o hau- |ighsi y |" \
            "u -Eotu|u-gi- |a hau -|ighsi y |" \
            "i- tuiO| ooo pas|--yayp| ood sff|" \
            "i- udPf| fgf d f|t s tasd| ssp ppd|" \
            "-t wuio| ooo pao)d)| s-yayp| ood sff|" \
            "--udso| fgf d f|t s yaod| sip fpd|" \
            "e eqroif|p-iy- |o hau- |igisi i |" \
            "o- Eouf|p-iy- |o hau- |igisi i |"
    # 左手
    left = "----|qtu qry |0ry 60t |9et 5qr |" \
           "8wty8 $ |48e 49r |37w 60t |9et 5qr |" \
           "8Wyi8- |8wu 7wy |60t 5 r |48e 59r |" \
           "8 tw8 t |3Wr0%0y |60e0$0e0|48q 269 |" \
           "59w 5-5|89yu7wry|60w05 r |48e459r9|" \
           "8wtw1 w8|7qe450r0|60e050r0|269249q9|" \
           "5-55 5 |48q849r9|379760t0|48e459w5|" \
           "1wyw1 $ |48q849r9|379760t0|48e459w |"
    # 右手拇指
    rightThumb = "----|p- a- |d- s- |s- p d |" \
                 "o- u Qf|p- a- |d- s- |s- p a |" \
                 "O- u- |----|--s d |----|" \
                 "--s f |----|o- I- |-- ff |" \
                 "--r- |----|--s d |----|" \
                 "--P ff|----|o- i t |-s -f |" \
                 "t t u s |f-gi -|d- s- |s s s a |" \
                 "s- u s |f-gi- |d- s- |s s s a |"
    # 右手食指
    rightIndexFinger = "----|f- d- |----|f- s- |" \
                       "f- -s |f- d- |----|f- s- |" \
                       "s- --|----|----|----|" \
                       "----|----|d- p- |----|" \
                       "----|----|----|----|" \
                       "--s -|----|d- p- |-q -y |" \
                       "----|--d -|----|f h -d |" \
                       "f ---|--d- |----|f h -d |"
    # 左手手拇指
    leftThumb = "----|-  -  |----|----|" \
                "--u -|----|----|----|" \
                "--W- |----|--0- |----|" \
                "w u E- |u y  uI |----|-e -q |" \
                "----|----|-t 0- |----|-u -E |" \
                "7 y % y |6-- e |----|" \
                "q--q5 |----|-w --|--q- |" \
                "----|----|-w --|--q- |"
    # 左手食指
    leftIndexFinger = "----|-  - |----|----|" \
                      "----|----|----|----|" \
                      "----|----|----|----|" \
                      "t- --|----|----|----|" \
                      "----|----|----|----|" \
                      "---t |----|----|----|" \
                      "----|----|----|----|" \
                      "----|----|----|----|"
    thread_play()
    # 右手
    right = ")d)y)o)d)s)#)u)s)a)y)u)a)s)y)u)s)|)d)y)o)d)s)u)o)s)a)y)u)a)s)y)u)s)|)f)I)p)f)d)i)p)d)s)i)p)s)d)i)p)d)|)f)i)p)f)d)i)p)d)a)i)p)a)a)i)p)a)|" \
            ")d)y)o)d)s)#)u)s)a)y)u)a)s)y)u)s)|)d)y)o)d)s)u)o)s)a)y)u)a)s)y)u)s)|)f)I)p)f)d)i)p)d)s)i)p)s)d)i)p)d)|)f)i)p)f)d)i)p)d)a)i)p)a)a)i)p)a)|"
    # 左手
    left = "10w010w0|60w060w0|$9Q949q9|269659q9|" \
           "10w010w0|60w060w0|$9Q949q9|26965- |"
    # 右手拇指
    rightThumb = "----|----|----|--g)#)g)f)#)f)|" \
                 "----|----|----|--g)#)g)f)#)f)|"
    # 右手食指
    rightIndexFinger = "----|----|----|----|" \
                       "----|----|----|----|"
    # 左手手拇指
    leftThumb = "8 t 8 t |6 t 6 t |-y -y |-q - r|" \
                "8 t 8 t |8 t 8 t |-y -y |-q 5 -|"
    # 左手食指
    leftIndexFinger = "----|----|----|----|" \
                      "----|----|----|----|"
    # 运行线程
    thread_play()
    # 右手
    right = "uosf|f-gi- |o hau- |ighsi y |" \
            "u -Eotu|u-gi- |a hau -|ighsi y |" \
            "oghss d |s i t -|"
    # 左手
    left = "--|qtu qry |0ry 60t |9et 5qr |" \
           "8wty8 $ |48e 49r |37w 60t |9et 5qr |" \
           "qtu wry |8wyw1 -|"
    # 右手拇指
    rightThumb = "--|p- a- |d- s- |s- p d |" \
                 "o- u Qf|p- a- |d- s- |s- p a |" \
                 "s ---|--u -|"

    # 右手食指
    rightIndexFinger = "--|f- d- |----|f- s- |" \
                       "f- -s |f- d- |----|f- s- |" \
                       "f ---|----| "
    # 左手手拇指
    leftThumb = "----|-  -  |----|----|" \
                "--u -|----|----|----|" \
                "----|----|"
    # 左手食指
    leftIndexFinger = "----|-  - |----|----|" \
                      "----|----|----|----|" \
                      "----|----|"
    thread_play()
